package com.example.xiaohei.ui

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.xiaohei.R
import com.example.xiaohei.databinding.ActivityMainBinding
import com.example.xiaohei.ui.fragment.AccountFragment
import com.example.xiaohei.ui.fragment.CategoryFragment
import com.example.xiaohei.ui.fragment.MyComicFragment
import com.example.xiaohei.ui.fragment.ReCommendFragment
import com.example.xiaohei.util.StatusBar
import com.example.xiaohei.viewmodel.MainViewModel
import com.google.android.material.bottomnavigation.BottomNavigationView


class MainActivity : AppCompatActivity(), LifecycleOwner {

    private val myComicFragment = MyComicFragment()
    private val reCommendFragment = ReCommendFragment()
    private val categoryFragment = CategoryFragment()
    private val accountFragment = AccountFragment()

    private var currentFragment: Fragment = reCommendFragment
    private var lifecycleRegistry = LifecycleRegistry(this)
    private lateinit var tvnum: TextView
    private lateinit var viewModel: MainViewModel
    private lateinit var binding: ActivityMainBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        //由于 启动时设置了 R.style.launcher 的windowBackground属性
        //势必要在进入主页后,把窗口背景清理掉
        setTheme(R.style.AppTheme);
        //启用沉浸式布局，白底黑字
        StatusBar.fitSystemBar(this);
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this,R.layout.activity_main)

        lifecycle.addObserver(reCommendFragment)//lifecycle的基本用法------------

        val navController = findNavController(R.id.nav_host_fragment)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.frg_recommend, R.id.frg_category, R.id.frg_my_comic, R.id.frg_account
            )
        )

        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)
        setupActionBarWithNavController(navController, appBarConfiguration)
        //通过setupWithNavController将底部导航和导航控制器进行绑定
        val bnv_view = findViewById<BottomNavigationView>(R.id.bnv_view)
        bnv_view.setupWithNavController(navController)
    }


}